﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace KeyCtrl
{
    class KeyCombo
    {
        protected List<Keys> _pressedKeyList;

        public Boolean Equals(KeyCombo combo)
        {
            return (combo.ToStoreString().Equals(ToStoreString()));
        }

        public KeyCombo(String storeString)
        {
            _pressedKeyList = new List<Keys>();

            //[todo] check if this is fast..., should really read a book about c#
            foreach (String k in storeString.Split('+'))
                _pressedKeyList.Add((Keys)System.Convert.ToInt32(k));
        }

        public KeyCombo()
        {
            _pressedKeyList = new List<Keys>();
        }

        new public String ToString()
        {
            String ret = "";
            foreach (Keys k in _pressedKeyList)
                ret += k.ToString() + "+";

            return Utils.TrimLast(ret);
        }

        public String ToStoreString()
        {
            String ret = "";
            foreach (Keys k in _pressedKeyList)
                ret += System.Convert.ToString((int)k) + "+";

            return Utils.TrimLast(ret);
        }

        public Boolean KeyDown(Keys key) //return true if the key wasn't pressed before therefore a change in the class state.
        {
            if (!_pressedKeyList.Contains(key))
            {
                _pressedKeyList.Add(key);
                return true;
            }

            return false;
        }

        public void KeyUp(Keys key)
        {
            _pressedKeyList.Remove(key);
        }
    }
}
